package cn.com.bluemoon.daps.common.enums;

import cn.com.bluemoon.daps.common.mp.BaseBmEnum;
import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.Optional;

/**
 *
 * @author Jarod.Kong
 * @date 2020/7/22 14:01
 */
public enum DatabaseType implements BaseBmEnum<Integer> {

    MYSQL(1, "mysql"),
    PGSQL(2, "postgresql"),
    POSTGRESQL(2, "postgresql"),
    HIVE(3, "hive"),
    HBASE(4, "hbase"),
    ES(5, "elasticsearch"),
    ELASTICSEARCH(5, "elasticsearch"),
    PHOENIX(6, "phoenix"),
    /**
     * Doris数据库鼻祖
     * 基于Doris数据库二开的产物，国内偏爱，多大厂参与，DorisDB改为为StarRocks
     * dbName，协议为mysql
     */
    DORIS_DB(7, "mysql"),
    IMPALA(8, "impala");

    @Setter
    @Getter
    private int code;
    /**
     * 定义数据库类型和jdbc连接数据库的名jdbc:${dbName}
     */
    @Setter
    @Getter
    private String dbName;

    DatabaseType(int code, String dbName) {
        this.code = code;
        this.dbName = dbName;
    }

    public static Optional<DatabaseType> matchOf(String name){
        if (name == null) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(d -> d.name().equals(name) || d.getDbName().equals(name)).findFirst();
    }

    @Override
    public Integer getValue() {
        return code;
    }
}
